package org.edu.modules.stu.zhsz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.utils.ConvertUtils;
import org.edu.common.base.BaseServiceImpl;
import org.edu.modules.stu.zhsz.entity.VeStuLiteraryActiveUser;
import org.edu.modules.stu.zhsz.entity.VeStuLiteraryActiveUser;
import org.edu.modules.stu.zhsz.entity.VeStuMoralActiveUser;
import org.edu.modules.stu.zhsz.mapper.VeStuLiteraryActiveMapper;
import org.edu.modules.stu.zhsz.entity.VeStuLiteraryActive;
import org.edu.modules.stu.zhsz.dto.VeStuLiteraryActiveDto;
import org.edu.modules.stu.zhsz.service.VeStuLiteraryActiveService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.edu.modules.stu.zhsz.service.VeStuLiteraryActiveUserService;
import org.edu.modules.stu.zhsz.service.VeStuLiteraryProgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;



/**
 * 实现类
 */
@Service
public class VeStuLiteraryActiveServiceImpl extends BaseServiceImpl<VeStuLiteraryActive, VeStuLiteraryActiveMapper> implements VeStuLiteraryActiveService {



    @Autowired
    private VeStuLiteraryActiveUserService userService;


    public IPage<VeStuLiteraryActiveDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper){
        return mapper.pageDto(page,queryWrapper);
    }
    /**
    * 查询实体类
    */
    public VeStuLiteraryActiveDto findDtoById(String id) {
        VeStuLiteraryActive entity = super.findById(id);
        VeStuLiteraryActiveDto dto = ConvertUtils.convertToDto(entity, VeStuLiteraryActiveDto.class);

        LambdaQueryWrapper<VeStuLiteraryActiveUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(VeStuLiteraryActiveUser::getUserId);
        lambdaQueryWrapper.eq(VeStuLiteraryActiveUser::getMcId,dto.getId());
        List<VeStuLiteraryActiveUser> veJkyOrganizeUsers = userService.listByWrapper(lambdaQueryWrapper);
        List<String> userIds = new ArrayList<>();
        for (VeStuLiteraryActiveUser user: veJkyOrganizeUsers) {
            userIds.add(user.getUserId());
        }
        dto.setUserIds(userIds);
        
        return dto;
    }

    /**
    * 新增或者修改
    */
    @Transactional
    public void insertOrUpdate(VeStuLiteraryActiveDto dto,Boolean isAdd){
        if(isAdd){
            super.insert(dto);
        }else{
            userService.deleteByPId(dto.getId());
            super.update(dto);
        }





        for (String userId:dto.getUserIds()) {
            VeStuLiteraryActiveUser moralProgUser = new VeStuLiteraryActiveUser();
            moralProgUser.setMcId(dto.getId());
            moralProgUser.setUserId(userId);
            userService.insert(moralProgUser);
        }
    }

    @Override
    public void importExcel(List<Map<String, Object>> mapList){
        for (Map map: mapList) {
            VeStuLiteraryActiveDto dto = new VeStuLiteraryActiveDto();
                   //         dto.setType ((String)map.get(0000));
                   //         dto.setTitle ((String)map.get(0000));
                   //         dto.setContent ((String)map.get(0000));
                   //         dto.setGrade ((String)map.get(0000));
                   //         dto.setCompany ((String)map.get(0000));
                   //         dto.setStatus ((String)map.get(0000));
                   //         dto.setPushTime ((String)map.get(0000));
                   //         dto.setStartTime ((String)map.get(0000));
                   //         dto.setEndTime ((String)map.get(0000));
                   //         dto.setPushUser ((String)map.get(0000));
                   //         dto.setPushUserName ((String)map.get(0000));
                   //         dto.setIsAll ((String)map.get(0000));
            
            super.insert(dto);
        }

    }

}
